<?php
namespace app\Sysadmin\Controller;
use think\Db;
class Adminuser extends Common{
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		$_input_data = input('get.');
		$_get_key = array('name','tel','email','username','role_id','status');
		$_get_str = "";
		
		if($_input_data){
			foreach($_input_data as $k=>$v){
				$_v = trim($v);
				if(in_array($k,$_get_key)){
					if($_v != ''){
						$_get_str .= $k.":'".strval($_v)."',";
					}else{
						$_input_data[$k] = '';
					}
				}else{
					unset($_input_data[$k]);
				}
			}
		}
		if($_get_key){
			foreach($_get_key as $a){
				if(!isset($_input_data[$a])){
					$_input_data[$a] = '';
				}
			}
		}
		$this->assign('get_data',$_input_data);
		$this->assign('role_lists',$this->get_role_lists());
		$this->assign('get_str',$_get_str);
		return $this->fetch();
	}
	public function add(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');
			
			$_data = array();
			$_data['role_id']	= (int)$_post_data['role_id'];
			$_data['username']	= trim($_post_data['username']);
			$_data['name']		= trim($_post_data['name']);
			$_data['tel']		= trim($_post_data['tel']);
			$_data['email']		= trim($_post_data['email']);
			$_data['status']	= $_post_data['status'];
			$_data['create_time'] = time();
			$_data['update_time'] = time();
			
			$_password = trim($_post_data['pass']);
			$_data['sign'] = rand(9999,1000);
			$_data['password'] = md5(md5($_password).$_data['sign']);
			
			if(!$_data['username']){
				$this->error('用户名不能为空！');exit;
			}
			if(Db::name('admin')->where('username','like',$_data['username'])->count()){
				$this->error('用户名重复！');exit;
			}
			if(!$_password){
				$this->error('密码不能为空！');exit;
			}
			// 密码格式验证
			
			$_ret_id = Db::name('admin')->insertGetId($_data);
			if($_ret_id){
				$this->add_operation_log($_ret_id,$_data);
				cache('admin_num_'.$_data['role_id'],null);
				$this->success("添加成功",url('index'));
			}else{
				$this->error("添加失败");
			}
			exit;
		}
		$_info = array(
			'id'=>'0',
			'role_id'=>'0',
			'username'=>'',
			'name'=>'',
			'tel'=>'',
			'email'=>'',
			'status'=>'1',
		);
		$this->assign('info',$_info);
		$this->assign('role_lists',$this->get_role_lists());
		$this->assign('page_name','添加管理员');
		return $this->fetch('info');
	}
	public function edit(){
		
		if($this->request->isPost() == true){
			$_post_data = input('post.');
			
			$_id = (int)$_post_data['id'];
			if(!$_id){
				$this->error('系统错误！');exit;
			}
			$_data = array();
			$_data['role_id']	= (int)$_post_data['role_id'];
			$_data['username']	= trim($_post_data['username']);
			$_data['name']		= trim($_post_data['name']);
			$_data['tel']		= trim($_post_data['tel']);
			$_data['email']		= trim($_post_data['email']);
			$_data['status']	= $_post_data['status'];
			$_data['update_time'] = time();
			
			$_password = trim($_post_data['pass']);
			if($_password){
				$_data['sign'] = rand(9999,1000);
				$_data['password'] = md5(md5($_password).$_data['sign']);
				// 密码格式验证
			}
			
			if(!$_data['username']){
				$this->error('用户名不能为空！');exit;
			}
			if(Db::name('admin')->where('id','<>',$_id)->where('username','like',$_data['username'])->count()){
				$this->error('用户名重复！');exit;
			}
			$_ret_id = Db::name('admin')->where('id','=',$_id)->update($_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_data);
				cache('admin_num_'.$_data['role_id'],null);
				$this->success("编辑成功",url('index'));
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_id = (int)(input('get.id'));
		$_info = Db::name('admin')->where('id','=',$_id)->find();
		if(!$_info){
			return $this->tips_404();exit;
		}
		
		$this->assign('info',$_info);
		$this->assign('role_lists',$this->get_role_lists());
		$this->assign('page_name','编辑管理员');
		return $this->fetch('info');
	}
	public function del(){
		if($this->request->isGet() == true){
			$_id = (int)input('get.id');
			if(!$_id){
				$this->error("请选择需要删除的信息！");exit;
			}
			$_info = Db::name('admin')->where('id','=',$_id)->find();
			if(!$_info){
				$this->error("请选择需要删除的信息！");exit;
			}
			$_ret = Db::name('admin')->where('id','=',$_id)->delete();
			if($_ret){
				$this->add_operation_log($_id,$_info);
				cache('admin_num_'.$_info['role_id'],null);
				$this->success("删除成功",url('index'));
			}else{
				$this->error("删除失败，请刷新以后重试！");
			}
			
		}else{
			$this->error("系统错误！");
		}
	}
	public function ajax_lists(){
		if($this->request->isAjax() != true){
			$this->error('404');
		}
		$_input_data = input();
		$page = $_input_data['page'] >= 1 ? (int)$_input_data['page'] : '0';
		$limit = $_input_data['limit'] >= 1 ? (int)$_input_data['limit'] : '15';
		unset($_input_data['page']);
		unset($_input_data['limit']);
		$_where = array();
		if(isset($_input_data['status'])){
			if($_input_data['status'] == '1'){
				$_where[] = array('Admin.status&AdminRole.status','=','1');
			}
			if($_input_data['status'] == '0'){
				$_where[] = array('Admin.status|AdminRole.status','=','0');
			}
		}
		if(isset($_input_data['role_id'])){
			$_where[] = array('Admin.role_id','=',$_input_data['role_id']);
			
		}
		if(!empty($_input_data['username'])){
			$_where[] = array('Admin.username','like','%'.$_input_data['username'].'%');
		}
		if(!empty($_input_data['name'])){
			$_where[] = array('Admin.name','like','%'.$_input_data['name'].'%');
		}
		if(!empty($_input_data['tel'])){
			$_where[] = array('Admin.tel','like','%'.$_input_data['tel'].'%');
		}
		if(!empty($_input_data['email'])){
			$_where[] = array('Admin.email','like','%'.$_input_data['email'].'%');
		}
		
		$count = Db::view('Admin','id')
					->view('AdminRole','', 'Admin.role_id=AdminRole.id')
					->where($_where)->count();
		$lists = Db::view('Admin','id,username,name,tel,email,status,create_time')
					->view('AdminRole',['name' => 'role_name', 'status' => 'role_status'], 'Admin.role_id=AdminRole.id')
					->where($_where)->select();
		if($lists){
			$role_lists = $this->get_role_lists();
			$role_lists = arrayKey($role_lists);
			foreach($lists as $k=>$v){
				$lists[$k]['create_date'] = date('Y-m-d H:i:s',$v['create_time']);
				unset($lists[$k]['create_time']);
			}
		}
		$_json = array();
		$_json['code'] = 0;
		$_json['msg'] = 'success';
		$_json['count'] = $count;
		$_json['data'] = $lists;
		echo json_encode($_json);
		exit;
	}
	private function get_role_lists(){
		$_lists = cache('sysadmin_role_lists');
		if($_lists){
			return $_lists;
		}
		$_lists = Db::name('admin_role')->field('id,name,info,status')->order('id asc')->select();
		cache('sysadmin_role_lists',$_lists,3600);
		return $_lists;
	}
}
?>